//
//  BHRouter.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/19.
//

import UIKit

class BHRouter: NSObject {
    
    static let shared: BHRouter = BHRouter.init()
    
    var logining: Bool = false
    
    /**去消息*/
    func bh_goMessage() {
        let messageVC = BHMessageMainViewController.init()
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(messageVC, animated: true)
        }
    }
    
    /**去用户的个人主页*/
    @objc func bh_goPersonalHome(userId: String?)  {
        let personalHomeVC = BHMePersonalHomeViewController.init()
        personalHomeVC.userid = userId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(personalHomeVC, animated: true)
        }
    }
    
    /**广告进入的活动页*/
    func bh_goActivity(activityId: String, title: String) {
        let activityVC = BHActivityViewController.init()
        activityVC.columnId = activityId
        activityVC.activityName = title
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(activityVC, animated: true)
        }
    }
    

    /**场馆详情*/
    func bh_goStadiumDetail(stadium: String)  {
        let detailVC = BHStadiumDetailViewController.init()
        detailVC.stadiumId = stadium
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    /**教练详情*/
    func bh_goCoachDetail(coachId: String) {
        let detailVC = BHCoachDetailViewController.init()
        detailVC.coachId = coachId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    /**商品详情*/
    func bh_goProductDetail(productId: String)  {
        let detailVC = BHStadiumProductDetailViewController.init()
        detailVC.productId = productId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    /**赛事详情*/
    func bh_goGameDetail(gameId: String)  {
        let detailVC = BHCompetitionDetailViewController.init()
        detailVC.gameId = gameId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    /**圈子详情*/
    func bh_goCircleDetail(circleId: String) {
        let detailVC = BHCircleDetailViewController.init()
        detailVC.circleId = circleId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    /**发布圈子*/
    func bh_goPublishCircle(activityId: String?) {
        let publishVC = BHCirclePublishViewController.init()
        if activityId != nil {
            if activityId?.isEmpty == false {
                publishVC.activityId = activityId ?? ""
            }
        }
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(publishVC, animated: true)
        }
    }
    
    /**趣玩详情*/
    func bh_goFunDetail(funId: String) {
        let detailVC = BHFunDetailViewController.init()
        detailVC.funId = funId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(detailVC, animated: true)
        }
    }
    
    /**运动打卡详情*/
    func bh_goSportSignDetail(targetId: String)  {
        let vc = BHSportClockDetailViewController.init()
        vc.targetId = targetId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(vc, animated: true)
        }
    }
    
    /**订单详情*/
    func bh_goOrderDetail(orderId: String)  {
        let vc = BHStadiumOrderDetailViewController.init()
        vc.orderId = orderId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(vc, animated: true)
        }
    }
    
    /**进入钱包*/
    func bh_goWallet()  {
        let vc = DPMeWalletViewController.init()
          DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(vc, animated: true)
        }
    }
    
    /**举报*/
    func bh_report(type: Int,businessId: String) {
        let reportVC = BHCommonReportViewController.init()
        reportVC.reportType = type
        reportVC.businessId = businessId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(reportVC, animated: true)
        }
    }

    
    /**用户列表 type */
    func bh_goUserList(type: Int,userId: String?,businessId: String?) {
        
        let userVC = BHCommonUserViewController.init()
        userVC.type = type
        userVC.userId = userId
        userVC.businessId = businessId
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(userVC, animated: true)
        }
    }
    /**用户收藏*/
    func bh_goUserCollection()  {
        let collectionVC = BHMeCollectionBaseViewController.init()
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(collectionVC, animated: true)
        }
        
    }
}
// MARK: 共用web
extension BHRouter {

    func bh_goWeb(title: String?,linkUrl: String) {
        if linkUrl.isEmpty == true {
            return
        }
        let webVC = BHCommonWebViewController.init()
        webVC.titleText = title ?? ""
        webVC.linkUrl = linkUrl
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(webVC, animated: true)
        }
    }
}

// MAKR: banner的点击
extension BHRouter {
    func bh_bannerClick(model: BHCommonBannerModel)  {
        if model.type == 1 { // 跳转原生
            self.analyzeJson(model: model)
        }
        if model.type == 2 { // 跳web
            self.bh_goWeb(title: model.title, linkUrl: model.link_url)
        }
    }
    
    private func analyzeJson(model: BHCommonBannerModel) {
        if model.business_json.isEmpty == true {
            return
        }
        let jsonDic = model.business_json.transJsonToDic()
        let type = BHJsonTool.decodeIntFromDic(dic: jsonDic, key: "type")
        let businessId = BHJsonTool.decodeStringFromDic(dic: jsonDic, key: "id")

        if type == 1 { // 赛事
            self.bh_goGameDetail(gameId: businessId)
        }
        if type == 2 { // 场馆
            self.bh_goStadiumDetail(stadium: businessId)
        }
        if type == 3 { // 教练
            self.bh_goCoachDetail(coachId: businessId)
        }
        if type == 4 { // 趣玩
            self.bh_goFunDetail(funId: businessId)
        }
        if type == 6 { // 活动
            let columId = BHJsonTool.decodeStringFromDic(dic: jsonDic, key: "columnId")
            let title = BHJsonTool.decodeStringFromDic(dic: jsonDic, key: "title")
            
            self.bh_goActivity(activityId: columId,title: title)
        }
    }
}

// 登录专属
extension BHRouter {
    func bh_gologin() {
        if self.logining == true {
            return
        }
        let loginVC = BHLoginViewController.init()
        let nav = BHNavigationController.init(rootViewController: loginVC)
        DispatchQueue.main.async {
            UIWindow.keyWindow.rootViewController = nav
            self.logining = true
        }
    }
}


// MARK: 创建聊天
extension BHRouter {
    func bh_createSingleChat(userId: String,name: String?)  {
        let conv = TUIConversationCellData.init()
        conv.userID = userId
        let chatVC = TUIChatController.init(conversation: conv)
        chatVC?.title = name
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(chatVC!, animated: true)
        }
    }
    
    func bh_goGroupChat(groupId: String)  {
        let conv = TUIConversationCellData.init()
        conv.groupID = groupId
        let chatVC = TUIChatController.init(conversation: conv)
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(chatVC!, animated: true)
        }
    }
    
    // 点击消息列表或者群聊的进入
    func bh_goChat(data: TUIConversationCellData!) {
        let chatVC = TUIChatController.init(conversation: data)
        chatVC?.title = data.title
        DispatchQueue.main.async {
            BHNavigationController.currentNavigationController?.pushViewController(chatVC!, animated: true)
        }
    }

}
